package com.weme.common.view

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.core.content.ContextCompat
import kotlin.math.min

interface CountdownListener {
    fun onCountdownFinished()
}
class CountdownProgressBar(context: Context, attrs: AttributeSet? = null) : View(context, attrs) {

    companion object {
        private const val MAX_PROGRESS = 60 // 倒计时的总时长（秒）
    }

    private val paint = Paint().apply {
        isAntiAlias = true
        strokeWidth = 8f // 设置圆环的宽度
        textSize = 26f // 设置文字大小
        textAlign = Paint.Align.CENTER // 设置文字居中对齐
        typeface = Typeface.create(Typeface.DEFAULT, Typeface.NORMAL)
        color = ContextCompat.getColor(context, com.weme.common.R.color.white)
    }
    private val whitePaint = Paint().apply {
        isAntiAlias = true
        style = Paint.Style.STROKE
        strokeWidth = 8f // 设置圆环的宽度
        color = ContextCompat.getColor(context, com.weme.common.R.color.white) // 未走完的颜色（白色）
    }

    private var currentProgress = MAX_PROGRESS // 当前剩余时间（秒）




    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        val centerX = width / 2
        val centerY = height / 2
        val radius = min(centerX, centerY) - 4 // 4 为圆环的宽度的一半

       // val progressAngle = (1 - currentProgress.toFloat() / MAX_PROGRESS) * 360 // 根据剩余时间计算进度对应的角度

        // 绘制未走完的白色圆环
     //   canvas.drawCircle(centerX.toFloat(), centerY.toFloat(), radius.toFloat(), whitePaint)

        // 绘制已走完的透明圆环
        canvas.drawArc(
            (centerX - radius).toFloat(),
            (centerY - radius).toFloat(),
            (centerX + radius).toFloat(),
            (centerY + radius).toFloat(),
            270 + mCurrentProgress,
            360 - mCurrentProgress,
            false,
            whitePaint
        )


        canvas.drawText(currentProgress.toString(), centerX.toFloat(), centerY.toFloat() - (paint.descent() + paint.ascent()) / 2, paint)
    }
    var valueAnnotation: ValueAnimator? = null
    var mCurrentProgress = 0f
    // 开始倒计时
    fun startCountdown( onCofirm: (() -> Unit) ) {
        valueAnnotation = getValA(MAX_PROGRESS * 1000)
        valueAnnotation?.addUpdateListener { animation ->
            val i = java.lang.Float.valueOf(animation.animatedValue.toString())
            mCurrentProgress = (360 * (i / 100f))
            val animatedValue = animation.animatedValue as Float
            val scaledValue = Math.round(animatedValue * 0.6f)
            currentProgress = MAX_PROGRESS - scaledValue
            invalidate()
        }
        valueAnnotation?.start()
        valueAnnotation?.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                onCofirm.invoke()
             //   mListener?.onCountdownFinished()
            }

        })


    }
    private fun getValA(countdownTime: Int = 60000): ValueAnimator? {
        val valueAnimator = ValueAnimator.ofFloat(0f, 100f)
        valueAnimator.duration = countdownTime.toLong()
        valueAnimator.interpolator = LinearInterpolator()
        valueAnimator.repeatCount = 0
        return valueAnimator
    }
    // 取消倒计时
    fun stopCountDown() {
        valueAnnotation?.pause()
        valueAnnotation?.clone()
    }
}

